/**
 * 面试题 05.03. 翻转数位
 */
public class ReverseBits {
    public static int reverseBits(int num) {
        int count1 = 0; // 记录当前0之前连续的一，即两个0之间连续的1
        int maxLen = 0; // 记录当前最长的1的个数
        int len = 0;
        String str = Integer.toBinaryString(num);
        System.out.println(Integer.toBinaryString(0));
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) != '0') {
                len ++;
                count1 ++;
            } else {
                len = count1 + 1;
                count1 = 0;
            }
            maxLen = Math.max(maxLen, len);
        }
        return Math.min(maxLen, 32);
    }

    public static void main(String[] args) {
        int num = 7;
        System.out.println(reverseBits(num));
    }
}
